A custom store in Svelte allows you to implement your own logic for subscribe, set, and update, instead of relying solely on writable or readable stores. It is useful for managing complex state, side effects, or asynchronous operations in a reusable way.
A custom store must have a subscribe method, and can optionally have other methods like set, update, or custom functions to manipulate state.
You have complex state logic involving multiple operations.
You want side effects like API calls, timers, or localStorage syncing handled inside the store.
You want reusable stores with helper methods beyond set and update.
You need derived or computed values tied to the store outside of component code.
Custom stores implement the subscribe contract and can include helper methods.
They encapsulate state logic and side effects outside of components.
They improve reusability and keep component code cleaner.
Custom stores can be global (shared across routes) or scoped to a component.